/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package carpool.services;

import carpool.persistence.fctuc.Users;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author sdinis
 */
public class FCTUCServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        String id = request.getParameter("id");
        //test to autocomplete
        String op = request.getParameter("operation");

        if ("finduser".equals(op)) {
            response.setContentType("text/xml");
            response.setHeader("Cache-Control", "no-cache");
            try {
                if (id == null || id.equals("")) {
                    out.write("<xml><error>Numero de estudante invalido</error></xml>");
                } else {
                    FCTUCUserBean userBean = new FCTUCUserBean();
                    Users u = userBean.getUser(Integer.parseInt(id));
                    //has inner protection on null
                    //out.write("<name>"+u.getFirstName()+" "+u.getLastName()+"</name>");
                    out.write(userBean.toXML(u));
                }
            } catch (Exception e) {
                out.write("<xml><error>" + e.getMessage() + "</error></xml>");
            } finally {
                out.close();
            }
        }
        out.write("<xml><error>" + "unknown error" + "</error></xml>");
        out.close();







    //HttpSession session = request.getSession();

    //para o login ou registo
            /*String firstName = request.getParameter("firstName");
    if ((firstName != null) && (!firstName.trim().equals(""))) {
    nameBean.setFirstName(firstName);
    }
    String lastName = request.getParameter("lastName");
    if ((lastName != null) && (!lastName.trim().equals(""))) {
    nameBean.setLastName(lastName);
    }

    String address = "/ShowName.jsp";
    RequestDispatcher dispatcher = request.getRequestDispatcher(address);
    dispatcher.forward(request, response);
     */
    }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
